package lcc.linkshare;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.Map.Entry;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;

public class Parser {

	
	public static void main(String[] args) throws MalformedURLException,
			IOException {
		InputStream stream = new URL("http://couponfeed.linksynergy.com/coupon?token=174fbd392ba9287fc5f3ae690ca92855ebf4403729e77c879fd0d5fa66b5051e").openStream();

		BufferedReader in = new BufferedReader(new InputStreamReader(stream));
		System.out.println("Connecting........");

		try {
			SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
			CouponFeedHandler couponFeedHandler = new CouponFeedHandler();
			saxParser.parse(new InputSource(in), couponFeedHandler);
			// Get link list
			List<Offer> offers = couponFeedHandler.getOfferList();
			// print link information
			for (Offer offer : offers) {
				ConnectionManager.insertOffer(offer);
			}
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("Connection Failed!!");
		}

	}

}
